#
import math
import numpy as np
import matplotlib.pyplot as plt
from irlab.basics.radar_equation import RadarEquation
from conf.app_config import AppConfig as AC

pt = 4 # peak power in Watts
freq = 94e+9 # radar operating frequency in Hz
tau = 50E-9
D = 12 # unit: in
g = 47.0 # antenna gain in dB
sigma = 20 # radar cross section in m squared
te = 290.0 # effective noise temperature in Kelvins
b = 20e+6 # radar operating bandwidth in Hz
nf = 7.0 # noise figure in dB
loss = 10.0 # radar losses in dB
range_ = np.linspace(1e3, 12e3, 1000) # range to target from 1. Km 12 Km, 1000 points
snr1 = RadarEquation.radar_equation(pt, freq, g, sigma, te, b, nf, loss, range_)
Rnewci = (94**0.25)*range_ # (94^0.25) .* range;
snrCI = snr1 + 10*np.log10(94) # 94 pulse coherent integration
delta_alpha = 240 # 正负120度范围
T_sc = 3 # 扫描时间为3秒
L_p = 2 # 2dB 用于考虑脉冲累积时的损失
PRF =10E3


lambda_ = AC.C / freq
B = 1 / tau
delta_R = AC.C * tau / 2.0
# 先将D由英吋变为米，然后将弧度变为角度
theta_3dB = 1.25 * (lambda_ / (D * 0.0254)) / np.pi * 180
dot_theta_scan = delta_alpha / T_sc
T_i = theta_3dB / dot_theta_scan
n_p = math.ceil(theta_3dB / dot_theta_scan * PRF)
print(f'lambda: {lambda_}; B: {B}; delta_R: {delta_R}; theta_3dB: {theta_3dB}; T_i: {T_i}; n_p: {n_p};')
snr_ref = 10
R_ref = RadarEquation.radar_equation_R_ref(pt, freq, g, sigma, te, b, nf, loss, snr_ref, tau)
R_CI = R_ref * (n_p)**0.25
print(f'R_ref: {R_ref}; R_CI: {R_CI};')
snr_CI_ = RadarEquation.radar_equation(pt, freq, g, sigma, te, b, nf, loss, R_CI)
print(f'snr_CI: {snr_CI_};')






# 非相参累积
SNR_NCI = 10 # dB
SNR_1 = SNR_NCI / (2*n_p) + np.sqrt( (SNR_NCI**2)/(4*n_p**2)  + (SNR_NCI)/(n_p))
SNR_1_db = 10*np.log10(SNR_1)
L_NCI = (1 + SNR_1) / SNR_1
L_NCI_db = 10*np.log10(L_NCI)
SNR_NCI2 = (n_p * SNR_1) / L_NCI
print(f'SNR_1: {SNR_1}; SNR_1_db: {SNR_1_db}; L_NCI: {L_NCI}; SNR_NCI2: {SNR_NCI2}; ???????????')
x159_db = 10*np.log10(n_p) - L_NCI_db
x159 = 10**(x159_db/10.0)
R_NCI = 2245 * (x159)**0.25
print(f'R_NCI: {R_NCI}; x159: {x159};')



# 绘制单脉冲SNR与距离的关系
rangekm = range_ / 1000
# 绘制单脉冲SNR与距离的关系
plt.figure(1)
plt.plot(rangekm, snr1, 'k-', label='single pulse')
# 绘制94脉冲CI SNR与距离的关系
plt.plot(Rnewci / 1000, snr1, 'k:', label='94 pulse CI')
# 设置坐标轴范围
plt.axis([1, 12, -20, 45])
# 显示网格
plt.grid(True)
# 添加图例
plt.legend()
# 添加轴标签
plt.xlabel('Detection range - Km')
plt.ylabel('SNR - dB')


# 计算 snr_b10
snr_b10 = 10 ** (snr1 / 10)
# 计算 SNR_1 使用方程 1.80
SNR_1 = snr_b10 / (2 * 94) + np.sqrt((snr_b10 ** 2) / (4 * 94 ** 2) + (snr_b10 / 94))
# 计算 LNCI 使用方程 1.78
LNCI = (1 + SNR_1) / SNR_1
# 计算 NCIgain
NCIgain = 10 * np.log10(94) - 10 * np.log10(LNCI)
# 计算 Rnewnci
Rnewnci = ((10 ** (0.1 * NCIgain) ) ** 0.25) * range_
# 计算 snrnci
snrnci = snr1 + NCIgain

# 绘制 SNR 与距离的关系图
plt.figure(2)
plt.plot(rangekm, snr1, 'k-', label='single pulse')           # 单脉冲
plt.plot(Rnewnci / 1000, snr1, 'k--', label='94 pulse NCI')   # 94脉冲 NCI
plt.plot(Rnewci / 1000, snr1, 'k:', label='94 pulse CI')      # 94脉冲 CI
# 设置坐标轴范围
plt.axis([1, 12, -20, 45])
# 显示网格
plt.grid(True)
# 添加图例
plt.legend()
# 添加轴标签
plt.xlabel('Detection range - Km')
plt.ylabel('SNR - dB')
# 显示图形
plt.show()